<template>
  <!-- 放置一个图标 -->
  <div>
    <!-- svg-icon渲染svg图标的全局组件
    icon-class传入svg文件名 -->
    <svg-icon
      :icon-class="isfull ? 'exit-fullscreen' : 'fullscreen'"
      class="fullscreen"
      @click="togg"
    />
  </div>
</template>

<script>
import sf from 'screenfull'
export default {
  name: 'ScreenFull',
  data () {
    return {
      isfull: false
    }
  },

  created () {
    sf.on('change', () => {
      console.log('全屏切换了', sf)
      this.isfull = sf.isFullscreen
    })
  },
  // 解绑事件
  beforeDestroy () {
    sf.off()
  },
  methods: {
    // 开启，关闭全屏
    togg () {
      if (!sf.isEnabled) {
        return this.$message.warning('浏览器不支持全屏')
      }
      // 自动切换
      sf.toggle()
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
